<?php

declare(strict_types=1);

namespace app\api\controller;

use think\facade\Request;
use think\facade\Db;




class Wechatoauth
{
    /**
     * 用户登录
     */
    public function accesstoken()
    {

        $APPID = 'wx5443073844f7418a'; //公众号AppID
        $APPSECRECT = '3ac73c94409b4a46e7ca4db9de53c02a'; //公众号密钥
        $code = Request::param('code');
        // 请求换取openID
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $APPID . '&secret=' . $APPSECRECT . '&code=' . $code . '&grant_type=authorization_code';
        // 创建一个新cURL资源
        $ch = curl_init();
        // 设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $url); //$url为访问的地址
        curl_setopt($ch, CURLOPT_HEADER, 0);
        // curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //传递头部信息
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回值不直接输出
        // 抓取URL并把它传递给浏览器
        $tokendata = json_decode(curl_exec($ch)); //把结果保存到变量
        // $tokendata = curl_exec($ch); //把结果保存到变量
        // 关闭cURL资源，并且释放系统资源
        curl_close($ch);
        // var_dump($tokendata);
        // die;
        // 换取用户信息
        $url1 = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $tokendata->access_token . '&openid=' . $tokendata->openid . '&lang=zh_CN';
        // 创建一个新cURL资源
        $ch1 = curl_init();
        // 设置URL和相应的选项
        curl_setopt($ch1, CURLOPT_URL, $url1); //$url为访问的地址
        curl_setopt($ch1, CURLOPT_HEADER, 0);
        // curl_setopt($ch1, CURLOPT_HTTPHEADER, $header); //传递头部信息
        curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); //返回值不直接输出
        // 抓取URL并把它传递给浏览器
        $userInfo = json_decode(curl_exec($ch1), true); //把结果保存到变量
        // 关闭cURL资源，并且释放系统资源
        curl_close($ch1);
        // var_dump($user_info);
        // var_dump($userInfo);
        // die;
        $user = Db::name('lq_user')->where('wechat_openid', $userInfo['openid'])->find();
        if (!$user) {
            $data = [
                'nickname' => userTextEncode($userInfo['nickname']),
                'avatarUrl' => $userInfo['headimgurl'],
                'province' => $userInfo['province'],
                'country' => $userInfo['country'],
                'city' => $userInfo['city'],
                'gender' => $userInfo['sex'],
                'wechat_openid' => $userInfo['openid'],
                'pay_money' => 0.00,
                'create_time' => time()
            ];
            Db::name('lq_user')->insert($data);
            $new_user = Db::name('lq_user')->where('wechat_openid', $userInfo['openid'])->find();
            $new_user['nickname'] = userTextDecode($new_user['nickname']);
            return jsonData(1, '登录成功', $new_user);
        } else {
            Db::name('lq_user')->where('wechat_openid', $userInfo['openid'])->update(['update_time' => time()]);
            $user['nickname'] = userTextDecode($user['nickname']);
            return jsonData(1, '登录成功', $user);
        }
    }
}
